package com.mf.service.authority.impl;

import com.mf.common.sso.LoginContext;
import com.mf.dao.authority.ResourceInfoDao;
import com.mf.dao.authority.RoleResourceRelationDao;
import com.mf.domain.authority.ResourceInfo;
import com.mf.domain.authority.RoleResourceRelation;
import com.mf.domain.general.tree.ZTree;
import com.mf.service.authority.RoleResourceRelationService;
import com.mf.service.framework.GenericService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 角色资源关联信息Service层实现类
 * User: zhaoming
 * DateTime: 2016-07-15
 * To change this template use File | Settings | File Templates.
 **/
@Service
public class RoleResourceRelationServiceImpl extends GenericService<RoleResourceRelation> implements RoleResourceRelationService {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private RoleResourceRelationDao roleResourceRelationDao;
    @Resource
    private ResourceInfoDao resourceInfoDao;

    /**
     * 新增角色资源映射
     * @param roleResourceRelation 角色资源映射Domain
     * @param resources 资源字符串
     */
    public boolean insert(RoleResourceRelation roleResourceRelation, String resources){
        boolean flag = true;
        try{

            String currentUserCode = LoginContext.getLoginUser().getUserCode();
            //删除当前角色拥有的资源
            List<RoleResourceRelation> roleResourceRelations = roleResourceRelationDao.selectByCondition(roleResourceRelation);
            for(RoleResourceRelation data : roleResourceRelations){
                roleResourceRelationDao.deleteByPrimaryKey(data);
            }

            if(StringUtils.isNotBlank(resources)){
                resources = resources.substring(0, resources.length()-1);
                String[] resourceCodes = resources.split(",");
                for(String resourceCode : resourceCodes){
                    RoleResourceRelation data = new RoleResourceRelation();
                    data.setResourceCode(resourceCode);
                    data.setRoleCode(roleResourceRelation.getRoleCode());
                    data.setCreateUser(currentUserCode);
                    data.setCreateTime(new Date());
                    data.setUpdateUser(currentUserCode);
                    data.setUpdateTime(new Date());
                    roleResourceRelationDao.insert(data);
                }
            }
        }catch (Exception e){
            flag = false;
            logger.error("新增失败", e);
        }
        return flag;
    }


    /**
     * 获取资源树
     * @param roleResourceRelation 角色资源关联信息Domain
     * @return 资源信息列表
     */
    public List<ZTree> getResourceTree(RoleResourceRelation roleResourceRelation){
        List<ZTree> allResourceInfo = new ArrayList<>();

        //获取当前角色拥有的资源并封装成MAP
        Map<String, Boolean> map = new HashMap<>();
        List<RoleResourceRelation> roleResourceRelations = roleResourceRelationDao.selectByCondition(roleResourceRelation);
        for(RoleResourceRelation data : roleResourceRelations){
            map.put(data.getResourceCode(), true);
        }

        ResourceInfo resourceInfo = new ResourceInfo();
        resourceInfo.setParentResourceCode("ROOT");
        //查询根目录的子节点
        List<ResourceInfo> resourceInfos = resourceInfoDao.treeGirdQuery(resourceInfo);
        for(ResourceInfo firstResourceInfo : resourceInfos){
            ZTree rootZTree = new ZTree();
            rootZTree.setId(firstResourceInfo.getId());
            rootZTree.setCode(firstResourceInfo.getResourceCode());
            rootZTree.setName(firstResourceInfo.getResourceName());
            rootZTree.setOpen(true);
            if(map.containsKey(firstResourceInfo.getResourceCode())){
                rootZTree.setChecked(true);
            }

            List<ZTree> firstChildren = new ArrayList();
            resourceInfo = new ResourceInfo();
            resourceInfo.setParentResourceCode(firstResourceInfo.getResourceCode());
            List<ResourceInfo> secondResourceInfoList = resourceInfoDao.treeGirdQuery(resourceInfo);
            for(ResourceInfo secondResourceInfo : secondResourceInfoList){
                ZTree secondZTree = new ZTree();
                secondZTree.setId(secondResourceInfo.getId());
                secondZTree.setCode(secondResourceInfo.getResourceCode());
                secondZTree.setName(secondResourceInfo.getResourceName());
                secondZTree.setOpen(true);
                if(map.containsKey(secondResourceInfo.getResourceCode())){
                    secondZTree.setChecked(true);
                }

                List<ZTree> secondChildren = new ArrayList();
                resourceInfo = new ResourceInfo();
                resourceInfo.setParentResourceCode(secondResourceInfo.getResourceCode());
                List<ResourceInfo> threeResourceInfoList = resourceInfoDao.treeGirdQuery(resourceInfo);
                for(ResourceInfo threeResourceInfo : threeResourceInfoList){
                    ZTree threeZTree = new ZTree();
                    threeZTree.setId(threeResourceInfo.getId());
                    threeZTree.setCode(threeResourceInfo.getResourceCode());
                    threeZTree.setName(threeResourceInfo.getResourceName());
                    if(map.containsKey(threeResourceInfo.getResourceCode())){
                        threeZTree.setChecked(true);
                    }

                    secondChildren.add(threeZTree);
                }
                secondZTree.setChildren(secondChildren);
                firstChildren.add(secondZTree);
            }
            rootZTree.setChildren(firstChildren);
            allResourceInfo.add(rootZTree);
        }

        return allResourceInfo;
    }


}
